import CardFunctions from "../Common/CardFunctions";
import { GameData } from "../Data/GameData";
import { BoardLayout } from "../Data/GameEnum";
import { ItemData } from "../Data/Level/ItemData";
import AssetsManager from "../Manager/AssetsManager";
import TimerManager from "../Manager/TimerManager";
import Card from "./Card";

const { ccclass, property } = cc._decorator;

/**Board */
@ccclass
export default class Board extends cc.Component {
    @property({ type: cc.Node })
    public Board_h: cc.Node = null;
    @property({ type: cc.Node })
    public Board_v: cc.Node = null;

    @property({ type: cc.Node })
    public cover_h: cc.Node = null;
    @property({ type: cc.Node })
    public cover_v: cc.Node = null;

    @property({ type: cc.Node })
    public collider_h: cc.Node = null;
    @property({ type: cc.Node })
    public collider_v: cc.Node = null;


    main_board: cc.Node;
    cover: cc.Node = null;
    collider: cc.Node = null;

    // is装填 = false;

    x: number = 0;
    y: number = 0;
    layer: number = 0;
    layout;

    data_cardList;
    cardList;

    isCover: boolean = false;

    length: number = 3;
    w: number = 0;
    h: number = 0;

    onLoad() {

    }

    Init(_x: number, _y: number, _layer: number, _layout: any, _data_cardList: any) {
        this.x = _x;
        this.y = _y;
        this.layer = _layer;
        this.layout = _layout;
        this.data_cardList = _data_cardList;

        console.log("this.data_cardList", this.data_cardList);

        this.node.x = this.x;
        this.node.y = this.y;

        if (this.layout === BoardLayout.h) {
            this.main_board = this.Board_h;
            this.cover = this.cover_h;
            this.collider = this.collider_h;
            this.w = 216;
            this.h = 72;
        }
        else if (this.layout === BoardLayout.v) {
            this.main_board = this.Board_v;
            this.cover = this.cover_v;
            this.collider = this.collider_v;
            this.w = 72;
            this.h = 216;
        }

        this.cardList = this.main_board.getComponentsInChildren(Card);
        for (let index = 0; index < _data_cardList.length; index++) {
            const id = _data_cardList[index];
            const item_card: Card = this.cardList[index];
            item_card.id = id;
            item_card.board = this;

            if (id >= 0) {
                const item_data = ItemData[id];
                item_card.Sprite_icon.spriteFrame = AssetsManager.图片[item_data.icon];
                item_card.Sprite_icon.node.scale = 1;
                item_card.Sprite_icon.node.y = 4;
            }
            else {
                // item_card.node.destroy();
                item_card.node.active = false;
            }
        }

        this.AddListener();
        this.Refresh();

        console.log("Board-Init");
    }

    AddListener() {
        this.main_board.on(cc.Node.EventType.TOUCH_START, this.TOUCH_START, this);
    }

    async TOUCH_START(t) {
        // const array = CardFunctions.GetHighLayerBoardArray(this);
        // // console.log("array", array);

        // const isBoardCover = CardFunctions.IsBoardCover(this);
        // console.log("isBoardCover", isBoardCover);

        // cc.tween(this.node)
        //     .to(0.1, { scale: 1.2 }, { easing: "" })
        //     .to(0.1, { scale: 1 }, { easing: "" })
        // .start();


        const array = CardFunctions.GetHighLayerBoardArray(this);
        for (let index = 0; index < array.length; index++) {
            const board = array[index];
            // console.log("是否相等", board === this);

            // let isBanana = CardFunctions.IsBanana(this, board);
            // console.log("isBanana", isBanana);

            // board.node.active = true;
            // if (isBanana) {
            cc.tween(board.node)
                .to(0.1, { scale: 1.2 }, { easing: "" })
                .to(0.1, { scale: 1 }, { easing: "" })
                .start();
            // }
        }
    }

    Refresh() {
        this.RefreshIcon();
        this.Refresh_layout();
        // this.RefreshCover();

        // this.cover.active = this.isCover;
        // const isShowFullCover = GameData.MapData_CurrentLevel[this.list_index].isShowFullCover;
        // this.Sprite_icon.node.active = !(this.isCover && isShowFullCover);
    }

    async Refresh_layout() {
        this.Board_h.active = false;
        this.Board_v.active = false;

        if (this.layout === BoardLayout.h) {
            this.Board_h.active = true;
        }
        else if (this.layout === BoardLayout.v) {
            this.Board_v.active = true;
        }
    }

    async RefreshIcon() {

    }

    async RefreshCover() {
        const isBoardCover = CardFunctions.IsBoardCover(this);
        this.cover.active = isBoardCover;
    }

    SetCover() {
        this.isCover = CardFunctions.IsBoardCover(this);
        this.RefreshCover();
    }

    Check撤凳子() {
        let count = 3// this.cardList.length;
        // console.log(`count----1`, count);

        for (let index = 0; index < this.cardList.length; index++) {
            const item_card: Card = this.cardList[index];
            // console.log(`item_card.id`, item_card.id);
            // console.log(`item_card.is装填`, item_card.is装填);

            if (item_card.id < 0 || item_card.is装填 === true) {
                count--;
            }
        }

        // console.log(`count----2`, count);


        if (count <= 0) {
            return true;
        }

        return false;
    }

    async Dead() {
        const i = GameData.arrBoard.indexOf(this);
        if (i > -1) {
            GameData.arrBoard.splice(i, 1);
        }

        this.cardList = [];

        cc.tween(this.node)
            .by(0.1, { scale: 0, y: 30 }, { easing: "" })
            .to(0.1, { scale: 0.4 }, { easing: "" })
            .call(() => {
                this.node.destroy();
            })
            .start();

        await TimerManager.AwaitTime(0.25);
    }
}